package com.example.bbs.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.bbs.entity.Doctor;
import com.example.bbs.entity.User;
import com.example.bbs.mapper.DoctorMapper;
import com.example.bbs.mapper.UserMapper;
import com.example.bbs.service.DoctorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiaziyun
 * @since 2021-04-30
 */
@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements DoctorService {
    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Doctor> getAllDoc() {
        return doctorMapper.getAllDoc();
    }


    @Override
    public String setDocInfoById(HashMap<String, String> parmmap) {
        if (parmmap == null || parmmap.size() == 0) {
            return "500";
        }
        try {
            String isallow = parmmap.get("isallow");
            String docid = parmmap.get("docid");
            User user = new User();
            user.setId(Integer.valueOf(docid));
            if ("1".equals(isallow)) {
                user.setType(2);
            } else if ("0".equals(isallow)) {
                user.setType(3);
            } else if ("2".equals(isallow)) {
                user.setType(4);
            }
            int i1 = userMapper.updateById(user);
            if (i1 <= 0) {
                return "500";
            }
            Doctor doctor = new Doctor();
            doctor.setId(Integer.valueOf(docid));
            doctor.setIsallow(Integer.valueOf(isallow));
            int i = doctorMapper.updateById(doctor);
            if (i > 0) {
                return "200";
            }
            return "500";
        } catch (Exception e) {
            return "500";
        }
    }

    @Override
    public String updateDocById(Doctor doctor) {
        doctor.setIsallow(2);
        int i = doctorMapper.updateById(doctor);
        if (i <= 0) {
            return "500";
        }
        User user = new User();
        user.setId(doctor.getId());
        user.setType(4);
        int i1 = userMapper.updateById(user);
        if (i1 > 0) {
            return "200";
        }
        return "500";
    }

    @Override
    public String certifDocById(Doctor doctor) {
        System.out.println(doctor.toString());
        //认证中
        doctor.setIsallow(2);
        User user = new User();
        user.setId(doctor.getId());
        //认证中
        user.setType(4);
        int i = 0;
        if (doctorMapper.selectById(doctor.getId()) != null) {
            i = doctorMapper.updateById(doctor);
        } else {
            i = doctorMapper.insert(doctor);
        }
        int i1 = userMapper.updateById(user);
        if (i > 0) {
            return "200";
        }
        return "500";
    }
}
